import QtQuick 2.0
//import QtWebKit 3.0

Flickable {
    id: myKit
    width: 1920
    height: 1080
    contentWidth: 1920
    contentHeight: 1080
    
    property string tempUnit: "F"
    property string distUnit: "mi/ft/in"
    property string speedUnit: "mph"
    property int speechDuration: 5000
    property string speechRecognizedLine: ""
    property var speechResultObj
    property var speechResultBorder
    property color speechResultSavedColor
    property bool speechIsSearchbox
    property string speechRequest
    property var selectedTODO
    
    function setGreenTheme() {
        // green color:  #FF38761D
        // green select: #FF274E13
        
        top_menu.color = "#FF38761D";
        content.color = "#FFD9EAD3";
        content.border.color = "#FF274E13";
        sidemenu.unselectColor = "#FF38761D";
        sidemenu.selectColor = "#FF274E13";
        help.color2 = "#FF38761D";
        recipesContent.selected = "#FF274E13";
        recipesContent.unselected = "#FF38761D";
        appliancesContent.selected = "#FF274E13";
        appliancesContent.unselected = "#FF38761D";
        appliancesContent.theme = "green";
        appliancesContent.setTheme();
        appliancesContent.checkThemeStatus();
        musicContent.theme = "green";
        musicContent.setTheme();
        calendarContent.theme = "green";
        calendarContent.setTheme();
        newsContent.selected = "#FF274E13";
        newsContent.unselected = "#FF38761D";
        sidemenu.updateColors();
        touchInput.source = "images/touch green.png"
        gestureInput.source = "images/gesture green.png"
        speechInput.source = "images/speech green.png"
        search_button.source = "images/bubbleGreen.png";
    }

    function setRedTheme() {
        // red color:  #FF990000
        // red select: #FF660000
        
        top_menu.color = "#FF990000";
        content.color = "#FFF4CCCC";
        content.border.color = "#FF660000";
        sidemenu.unselectColor = "#FF990000";
        sidemenu.selectColor = "#FF660000";
        help.color2 = "#FF990000";
        recipesContent.selected = "#FF660000";
        recipesContent.unselected = "#FF990000";
        appliancesContent.selected = "#FF660000";
        appliancesContent.unselected = "#FF990000";
        appliancesContent.theme = "red";
        appliancesContent.setTheme();
        appliancesContent.checkThemeStatus();
        musicContent.theme = "red";
        musicContent.setTheme();
        calendarContent.theme = "red";
        calendarContent.setTheme();
        newsContent.selected = "#FF660000";
        newsContent.unselected = "#FF990000";
        sidemenu.updateColors();
        touchInput.source = "images/touch red.png"
        gestureInput.source = "images/gesture red.png"
        speechInput.source = "images/speech red.png"
        search_button.source = "images/bubbleRed.png";
    }

    function setBlueTheme() {
        // blue color:  #FF0B5394
        // blue select: #FF073763
        
        top_menu.color = "#FF0B5394";
        content.color = "#FFCFE2F3";
        content.border.color = "#FF073763";
        sidemenu.unselectColor = "#FF0B5394";
        sidemenu.selectColor = "#FF073763";
        help.color2 = "#FF0B5394";
        recipesContent.selected = "#FF073763";
        recipesContent.unselected = "#FF0B5394";
        appliancesContent.selected = "#FF073763";
        appliancesContent.unselected = "#FF0B5394";
        appliancesContent.theme = "blue";
        appliancesContent.setTheme();
        appliancesContent.checkThemeStatus();
        musicContent.theme = "blue";
        musicContent.setTheme();
        calendarContent.theme = "blue";
        calendarContent.setTheme();
        newsContent.selected = "#FF073763";
        newsContent.unselected = "#FF0B5394";
        sidemenu.updateColors();
        touchInput.source = "images/touch blue.png"
        gestureInput.source = "images/gesture blue.png"
        speechInput.source = "images/speech blue.png"
        search_button.source = "images/bubbleBlue.png";
    }

    function scanBarcode(image) {
        barcodeScanner.recognize(image);
    }


    function startSpeechRecognition(_speechRequest, _isSearchBox, _speechResultObj, _speechResultBorder, _speechResultSavedColor,_speechDuration) {
        speechRequest = _speechRequest;
        speechIsSearchbox = _isSearchBox;
        speechResultObj = _speechResultObj;
        speechResultBorder = _speechResultBorder;
        speechDuration = _speechDuration;
        speechResultSavedColor = _speechResultSavedColor;

        speechTimerTrigger.start();
    }

    function startSpeechRecognitionReq(_speechRequest, _speechDuration) {
        speechRequest = _speechRequest;
        speechDuration = _speechDuration;

        speechTimerTrigger.start();
    }

    function speechRecognize() {
        speechRecognizedLine = voiceCommands.recognize();
        search_text.text = speechRecognizedLine.toLowerCase();

        if( speechRequest === "recipe" ) {
            recipesContent.loadRecipes(speechRecognizedLine);
        }
        else if( speechRequest == "notes" ) {
            stickyContent.addNote(speechRecognizedLine, false);
        }
        else if( speechRequest == "todo" ) {
            stickyContent.addNote(speechRecognizedLine, true);
        }
        else if( speechRequest == "todoItem" ) {
            selectedTODO.addItem(speechRecognizedLine, true);
        }
        else if( speechRequest == "calendar" ) {
            console.log("We are in Mykit, creating a new entry!");
            var dateTime = new Date();
            calendarContent.creatEvents(calendarContent.clickedDay, calendarContent.currentMonth, calendarContent.currentYear, speechRecognizedLine, dateTime);
        }
        else if( speechRequest == "user" ) {
            userSelection.addUser(speechRecognizedLine);
        }
        else if( speechRequest == "editUser" ) {
            speechResultObj.text = speechRecognizedLine;
            speechResultBorder.color = speechResultSavedColor;
            console.log("logins/" + speechResultObj.parent.parent.id2 + "/userName");
            settings.setValue("logins/" + speechResultObj.parent.parent.id2 + "/userName", speechRecognizedLine);
        }
        else if( speechRequest == "twitter") {
            speechResultObj.speechText = speechRecognizedLine;
            speechResultBorder.color = speechResultSavedColor;

        }
        else if( speechRequest == "flickr" ) {
            speechResultObj.speechText = speechRecognizedLine;
            speechResultBorder.color = speechResultSavedColor;

        }
        else if( speechRequest == "zipcode" ) {
            speechResultObj.speechText = speechRecognizedLine;
            speechResultBorder.color = speechResultSavedColor;

        }
        else if( speechRequest == "speechBar" ) {

            switch( speechRecognizedLine.toLowerCase() ) {
            case "open maps":
            case "open map":
            case "select maps":
            case "select map":
                sidemenu.unselectAllSideButtons();
                sidemenu.selectSideButton("map");
                sidemenu.setVisible(mapContent);
                break;
            case "open recipes":
            case "select recipes":
                sidemenu.unselectAllSideButtons();
                sidemenu.selectSideButton("recipes");
                sidemenu.setVisible(recipesContent);
                break;
            case "open appliances":
            case "select appliances":
                sidemenu.unselectAllSideButtons();
                sidemenu.selectSideButton("appliances");
                sidemenu.setVisible(appliancesContent);
                break;
            case "open calendar":
            case "select calendar":
                sidemenu.unselectAllSideButtons();
                sidemenu.selectSideButton("calendar");
                sidemenu.setVisible(calendarContent);
                break;
            case "open social":
            case "select social":
                sidemenu.unselectAllSideButtons();
                sidemenu.selectSideButton("social");
                sidemenu.setVisible(socialContent);
                break;
            case "open music":
            case "select music":
                sidemenu.unselectAllSideButtons();
                sidemenu.selectSideButton("music");
                sidemenu.setVisible(musicContent);
                break;
            case "open news":
            case "select news":
                sidemenu.unselectAllSideButtons();
                sidemenu.selectSideButton("news");
                sidemenu.setVisible(newsContent);
                break;
            case "open weather":
            case "select weather":
                sidemenu.unselectAllSideButtons();
                sidemenu.selectSideButton("weather");
                sidemenu.setVisible(weatheContent);
                break;
            case "open games":
            case "select games":
                sidemenu.unselectAllSideButtons();
                sidemenu.selectSideButton("games");
                sidemenu.setVisible(gameContent);
                break;
            case "open notes":
            case "open sticky notes":
            case "open todo":
            case "select notes":
            case "select sticky notes":
            case "select todo":
                sidemenu.unselectAllSideButtons();
                sidemenu.selectSideButton("sticky");
                sidemenu.setVisible(stickyContent);
                break;
            case "open grocery list":
            case "select grocery list":
                sidemenu.unselectAllSideButtons();
                sidemenu.selectSideButton("groceries");
                sidemenu.setVisible(groceriesContent);
                break;
            case "open settings":
            case "select settings":
                sidemenu.unselectAllSideButtons();
                sidemenu.selectSideButton("settings");
                sidemenu.setVisible(settingsContent);
                break;
            case "open help":
            case "select help":
                sidemenu.unselectAllSideButtons();
                sidemenu.selectSideButton("help");
                sidemenu.setVisible(helpContent);
                break;
            }
        }
    }
    
    Column {
        spacing: 15
        Rectangle {
            id: top_menu
            width: 1920
            height: 100
            color: "#FF38761D"
            Row {
                id: signOutRegion
                anchors.left: parent.left
                anchors.leftMargin: 4
                anchors.verticalCenter: parent.verticalCenter
                spacing: 10
                Image {
                    id: signOut
                    anchors.verticalCenter: parent.verticalCenter
                    source: "images/login face.png"
                    height: 92
                    width: 72
                }
                Text {
                    anchors.verticalCenter: parent.verticalCenter
                    text: "Sign Out"
                    font.family: "Verdana"
                    font.pointSize: 24
                    color: "#FFFFFFFF"
                }
            }
            MouseArea {
                id: signOutClick
                anchors.fill: signOutRegion
                onClicked: { 
                    sidemenu.unselectAllSideButtons();
                    sidemenu.selectSideButton("favorites");
                    sidemenu.setVisible(userSelection);
                    myKit.setGreenTheme();
                }
            }
        }
        Row {
            spacing: 0
            SideButtonMenu{
                id: sidemenu
                unselectColor: "#FF38761D"
                selectColor: "#FF274E13"
            }
            Rectangle {
                id: content
                width: 1680
                height: 830
                color: "#FFD9EAD3"
                border { width: 5; color: "#FF274E13"; }
                
                UserSelector {
                    id: userSelection
                    anchors.top: parent.top
                    anchors.topMargin: 5
                    anchors.left: parent.left
                    anchors.leftMargin: 5
                    width: parent.width - 10
                    height: parent.height - 10
                    visible: true
                }
                CustomWebView {
                    id: webpage
                    anchors.top: parent.top
                    anchors.topMargin: 5
                    anchors.left: parent.left
                    anchors.leftMargin: 5
                    width: parent.width - 10
                    height: parent.height - 10
                    scale: 1.0
                    smooth: false
                    clip: true
                    focus: true
                    visible: false
                    url: "loading.html"
                }
                
                Clock {
                    id: clockContent
                    anchors.top: parent.top
                    anchors.topMargin: 5
                    anchors.left: parent.left
                    anchors.leftMargin: 5
                    width: parent.width - 10
                    height: parent.height - 10
                    visible: false
                }
                
                Recipes{
                   id: recipesContent
                   anchors.top: parent.top
                   anchors.topMargin: 5
                   anchors.left: parent.left
                   anchors.leftMargin: 5
                   width: parent.width - 10
                   height: parent.height - 10
                   visible: false
                }

                Appliances{
                   id: appliancesContent
                   anchors.top: parent.top
                   anchors.topMargin: 5
                   anchors.left: parent.left
                   anchors.leftMargin: 5
                   width: parent.width - 10
                   height: parent.height - 10
                   visible: false
                }

                Calendar {
                    id: calendarContent
                    anchors.top: parent.top
                    anchors.topMargin: 5
                    anchors.left: parent.left
                    anchors.leftMargin: 5
                    width: parent.width - 10
                    height: parent.height - 10
                    visible: false
                }
                
                SocialMedia {
                    id: socialContent
                    anchors.top: parent.top
                    anchors.topMargin: 5
                    anchors.left: parent.left
                    anchors.leftMargin: 5
                    width: parent.width - 10
                    height: parent.height - 10
                    visible: false
                }
                
                MapSelector {
                    id: mapContent
                    anchors.top: parent.top
                    anchors.topMargin: 5
                    anchors.left: parent.left
                    anchors.leftMargin: 5
                    width: parent.width - 10
                    height: parent.height - 10
                    visible: false
                }
                
                Music {
                    id: musicContent
                    anchors.top: parent.top
                    anchors.topMargin: 5
                    anchors.left: parent.left
                    anchors.leftMargin: 5
                    width: parent.width - 10
                    height: parent.height - 10
                    visible: false
                }
                
                NewsSelector {
                    id: newsContent
                    anchors.top: parent.top
                    anchors.topMargin: 5
                    anchors.left: parent.left
                    anchors.leftMargin: 5
                    width: parent.width - 10
                    height: parent.height - 10
                    visible: false
                }

                Weather {
                    id: weatherContent
                    anchors.top: parent.top
                    anchors.topMargin: 5
                    anchors.left: parent.left
                    anchors.leftMargin: 5
                    width: parent.width - 10
                    height: parent.height - 10
                    visible: false
                    tempUnit: myKit.tempUnit
                    distUnit: myKit.distUnit
                    speedUnit: myKit.speedUnit
                }
                
                GameSelector {
                    id: gameContent
                    anchors.top: parent.top
                    anchors.topMargin: 5
                    anchors.left: parent.left
                    anchors.leftMargin: 5
                    width: parent.width - 10
                    height: parent.height - 10
                    visible: false
                }
                
                Notes {
                    id: stickyContent
                    anchors.top: parent.top
                    anchors.topMargin: 5
                    anchors.left: parent.left
                    anchors.leftMargin: 5
                    width: parent.width - 10
                    height: parent.height - 10
                    visible: false
                }
                
                SettingSelector {
                    id: settingsContent
                    anchors.top: parent.top
                    anchors.topMargin: 5
                    anchors.left: parent.left
                    anchors.leftMargin: 5
                    width: parent.width - 10
                    height: parent.height - 10
                    visible: false
                }
                Groceries {
                    id: groceriesContent
                    anchors.top: parent.top
                    anchors.topMargin: 5
                    anchors.left: parent.left
                    anchors.leftMargin: 5
                    width: parent.width - 10
                    height: parent.height - 10
                    visible: false
                }
            }
        }
        Item {
            width: 200
            height: 15
        }
        Row {
            spacing: 30
            SideButton {
                id: help
                text: "Help"
                source: "images/help_icon.png"
                color2: "#FF38761D"
                onClicked: {
                    // open help pop-up
                    //webpage.goBack();
                    helpContent.visible = true;
                    if (userSelection.visible === true) {
                        helpContent.children[2].visible = false; //calendar
                        helpContent.children[4].visible = false; //music
                        helpContent.children[5].visible = false; //maps, news, games
                        helpContent.children[6].visible = false; //recipes
                        helpContent.children[7].visible = false; //Appliances
                        helpContent.children[8].visible = false; //social
                        helpContent.children[9].visible = false; //notes
                        helpContent.children[10].visible = false;
                        helpContent.children[11].visible = false;
                    }
                    if (webpage.visible === true) {
                        helpContent.children[2].visible = false; //calendar
                        helpContent.children[4].visible = false; //music
                        helpContent.children[5].visible = false; //maps, news, games
                        helpContent.children[6].visible = false; //recipes
                        helpContent.children[7].visible = false; //Appliances
                        helpContent.children[8].visible = false; //social
                        helpContent.children[9].visible = false;
                        helpContent.children[10].visible = false;
                        helpContent.children[11].visible = false;
                    }
                    if (clockContent.visible === true) {
                        helpContent.children[2].visible = false;
                        helpContent.children[4].visible = false;
                        helpContent.children[5].visible = false;
                        helpContent.children[6].visible = false;
                        helpContent.children[7].visible = false;
                        helpContent.children[8].visible = false;
                        helpContent.children[9].visible = false;
                        helpContent.children[10].visible = false;
                        helpContent.children[11].visible = false;
                    }
                    if (recipesContent.visible === true) {
                        helpContent.children[2].visible = false;
                        helpContent.children[4].visible = false;
                        helpContent.children[5].visible = false;
                        helpContent.children[6].visible = true;
                        helpContent.children[7].visible = false;
                        helpContent.children[8].visible = false;
                        helpContent.children[9].visible = false;
                        helpContent.children[10].visible = false;
                        helpContent.children[11].visible = false;
                    }
                    if (appliancesContent.visible === true) {
                        helpContent.children[2].visible = false;
                        helpContent.children[4].visible = false;
                        helpContent.children[5].visible = false;
                        helpContent.children[6].visible = false;
                        helpContent.children[7].visible = true;
                        helpContent.children[8].visible = false;
                        helpContent.children[9].visible = false;
                        helpContent.children[10].visible = false;
                        helpContent.children[11].visible = false;
                    }
                    if (calendarContent.visible === true) {
                        helpContent.children[2].visible = true;
                        helpContent.children[4].visible = false;
                        helpContent.children[5].visible = false;
                        helpContent.children[6].visible = false;
                        helpContent.children[7].visible = false;
                        helpContent.children[8].visible = false;
                        helpContent.children[9].visible = false;
                        helpContent.children[10].visible = false;
                        helpContent.children[11].visible = false;
                    }
                    if (socialContent.visible === true) {
                        helpContent.children[2].visible = false;
                        helpContent.children[4].visible = false;
                        helpContent.children[5].visible = false;
                        helpContent.children[6].visible = false;
                        helpContent.children[7].visible = false;
                        helpContent.children[8].visible = true;
                        helpContent.children[9].visible = false;
                        helpContent.children[10].visible = false;
                        helpContent.children[11].visible = false;
                    }
                    if (mapContent.visible === true) {
                        helpContent.children[2].visible = false;
                        helpContent.children[4].visible = false;
                        helpContent.children[5].visible = true;
                        helpContent.children[6].visible = false;
                        helpContent.children[7].visible = false;
                        helpContent.children[8].visible = false;
                        helpContent.children[9].visible = false;
                        helpContent.children[10].visible = false;
                        helpContent.children[11].visible = false;
                    }
                    if (musicContent.visible === true) {
                        helpContent.children[2].visible = false;
                        helpContent.children[4].visible = true;
                        helpContent.children[5].visible = false;
                        helpContent.children[6].visible = false;
                        helpContent.children[7].visible = false;
                        helpContent.children[8].visible = false;
                        helpContent.children[9].visible = false;
                        helpContent.children[10].visible = false;
                        helpContent.children[11].visible = false;
                    }
                    if (newsContent.visible === true) {
                        helpContent.children[2].visible = false;
                        helpContent.children[4].visible = false;
                        helpContent.children[5].visible = true;
                        helpContent.children[6].visible = false;
                        helpContent.children[7].visible = false;
                        helpContent.children[8].visible = false;
                        helpContent.children[9].visible = false;
                        helpContent.children[10].visible = false;
                        helpContent.children[11].visible = false;
                    }
                    if (weatherContent.visible === true) {
                        helpContent.children[2].visible = false;
                        helpContent.children[4].visible = false;
                        helpContent.children[5].visible = false;
                        helpContent.children[6].visible = false;
                        helpContent.children[7].visible = false;
                        helpContent.children[8].visible = false;
                        helpContent.children[9].visible = false;
                        helpContent.children[10].visible = false;
                        helpContent.children[11].visible = false;
                    }
                    if (gameContent.visible === true) {
                        helpContent.children[2].visible = false;
                        helpContent.children[4].visible = false;
                        helpContent.children[5].visible = true;
                        helpContent.children[6].visible = false;
                        helpContent.children[7].visible = false;
                        helpContent.children[8].visible = false;
                        helpContent.children[9].visible = false;
                        helpContent.children[10].visible = false;
                        helpContent.children[11].visible = false;
                    }
                    if (stickyContent.visible === true) {
                        helpContent.children[2].visible = false;
                        helpContent.children[4].visible = false;
                        helpContent.children[5].visible = false;
                        helpContent.children[6].visible = false;
                        helpContent.children[7].visible = false;
                        helpContent.children[8].visible = false;
                        helpContent.children[9].visible = true;
                        helpContent.children[10].visible = false;
                        helpContent.children[11].visible = false;
                    }
                    if (groceriesContent.visible === true) {
                        helpContent.children[2].visible = false;
                        helpContent.children[4].visible = false;
                        helpContent.children[5].visible = false;
                        helpContent.children[6].visible = false;
                        helpContent.children[7].visible = false;
                        helpContent.children[8].visible = false;
                        helpContent.children[9].visible = false;
                        helpContent.children[10].visible = true;
                        helpContent.children[11].visible = false;
                    }
                    if (settingsContent.visible === true) {
                        helpContent.children[2].visible = false;
                        helpContent.children[4].visible = false;
                        helpContent.children[5].visible = false;
                        helpContent.children[6].visible = false;
                        helpContent.children[7].visible = false;
                        helpContent.children[8].visible = false;
                        helpContent.children[9].visible = false;
                        helpContent.children[10].visible = false;
                        helpContent.children[11].visible = true;
                    }
                }
            }

            /* The voicecommander needs to be in MyKit.qml. Don't move it in anoither file,
             * it will stop working!*/
            Row {
                spacing: 10
                Rectangle {
                    id: search_rect

                    border.color: content.border.color
                    border.width: 2
                    color: "transparent"
                    width: 1000
                    height: 50
                    radius: 8

                    Text {
                        id: search_text

                        x: 12
                        text: "Ex.: Open Maps"
                        font.italic: true
                        font.pointSize: 20
                        font.family: "Verdana"
                        color: "gray"
                        anchors.verticalCenter: parent.verticalCenter
                    }
                }


                Image {
                    id: search_button
                    //border.color: "#FF333333"
                    //border.width: 2
                    y: -15
                    width: 128
                    height: 128
                    source: "images/bubbleGreen.png"

                    MouseArea {
                        anchors.fill: parent
                        onClicked: {
                            startSpeechRecognitionReq("speechBar", 3000);
                        }
                    }
                }

                Item{
                    width: 60
                    height: 20
                }

                Image {
                    id: touchInput
                    anchors.top: parent.top
                    anchors.topMargin: -44
                    source: "images/touch green.png"
                    width: 128
                    height: 128
                }
                Image {
                    id: gestureInput
                    anchors.top: parent.top
                    anchors.topMargin: -44
                    source: "images/gesture green.png"
                    width: 128
                    height: 128
                }
                Image {
                    id: speechInput
                    anchors.top: parent.top
                    anchors.topMargin: -44
                    source: "images/speech green.png"
                    width: 128
                    height: 128
                }
            }
        }
    }

    Help {
        id: helpContent
        width: parent.width
        height: parent.height
        visible: false
    }

    Timer {
        id: speechTimerTrigger
        interval: myKit.speechDuration

        running: false
        repeat: false
        triggeredOnStart: true

        property var loadingBar

        onTriggered: {
            if( running ) {
                var component = Qt.createComponent("SpeechLoadingBar.qml");
                loadingBar = component.createObject(search_rect);
                loadingBar.width = 1000;

                voiceCommands.startRecording();
            } else if( !running ) {
                voiceCommands.stopRecording();
                speechRecognize();
                loadingBar.destroy();
            }
        }
    }

    function loadSettings(user) {
        userSelection.loggedUser = user;
        var theme = settings.getValue("settings/" + user + "/theme");
        switch( theme ) {
        case "blueTheme":
            setBlueTheme();
            break;
        case "greenTheme":
            setGreenTheme();
            break;
        case "redTheme":
            setRedTheme();
            break;
        default:
            setGreenTheme();
        }

        myKit.speedUnit = settings.getValue("settings/" + userSelection.loggedUser + "/speedUnit", "mph");
        myKit.tempUnit = settings.getValue("settings/" + userSelection.loggedUser + "/tempUnit", "F");
        myKit.distUnit = settings.getValue("settings/" + userSelection.loggedUser + "/distanceUnit", "mi/ft/in");
        
        appliancesContent.temperatureUnit = myKit.tempUnit;
        appliancesContent.initializeTemp();

        weatherContent.zipcode = settings.getValue("settings/" + userSelection.loggedUser + "/zipcode", "60657");
        weatherContent.updateLocation();
    }

}


